package com.qf.servlet;

import com.qf.utils.DemoUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/register")
public class RegisterServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String againpassword = req.getParameter("againpassword");
        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        String birthday = req.getParameter("birthday");
        String[] powers = req.getParameterValues("power");
        String jiguan = req.getParameter("jiguan");

        if (password == null || password.equals("") || !password.equals(againpassword)) {

            resp.sendRedirect("register.jsp?status=1");
            return;
        }

        if (username == null || username.equals("")) {

            resp.sendRedirect("register.jsp?status=2");
            return;
        }


        Admin admin = new Admin();
        admin.setUsername(username);
        admin.setPassword(password);
        admin.setName(name);
        admin.setSex(sex);
        admin.setBirthday(birthday);
        admin.setPower(powers);
        admin.setJiguan(jiguan);

        DemoUtils.addAdmin(admin);
        resp.sendRedirect("login.jsp");
    }
}